﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace блокнот1
{
    public partial class Form1 : Form
    {
        Note text;

        public Form1()
        {
            text = new Note();

            InitializeComponent();

            CancelToolStripMenuItem.Enabled = false;
            CutToolStripMenuItem.Enabled = false;
            if (!Clipboard.ContainsText()) PutToolStripMenuItem.Enabled = false;
            DeleteToolStripMenuItem.Enabled = false;

        }

        private void создатьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                textBox.Clear();
                text.Clear();
                CancelToolStripMenuItem.Enabled = false;
            }
        }

        private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                OpenFileDialog openFile = new OpenFileDialog();
                openFile.CheckFileExists = true;
                openFile.CheckPathExists = true;
                if (openFile.ShowDialog() == DialogResult.OK)
                {
                    using (var reader = new StreamReader(openFile.FileName, Encoding.Default))
                    {
                        text.fileName = openFile.FileName;
                        text.originalString = reader.ReadToEnd();
                        textBox.Text = text.originalString;
                    }
                }
            }
        }

        private void сохранитьКакToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void выйтиToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Check()) Application.Exit();
        }

        private void отменитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!text.cancelEdit)
            {
                text.editString = textBox.Text;
                textBox.Text = text.originalString;
                text.cancelEdit = true;
            }
            else
            {
                textBox.Text = text.editString;
                text.cancelEdit = false;
            }
        }

        private void вырезатьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox.Cut();
        }

        private void вставитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox.Text.Insert(textBox.SelectionStart, Clipboard.GetText());
        }

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox.Text = textBox.Text.Remove(textBox.SelectionStart, textBox.SelectionLength);
        }

        private void шрифтToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        //---------------------------------------------------------------------

        private bool Save()
        {
            text.editString = textBox.Text;
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Title = "Сохранить как...";
            saveFile.FileName = text.fileName;
            saveFile.Filter = "Текстовые файлы (*.txt)|*.txt|Все файлы (*.*)|*.*";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                using (var writer = new StreamWriter(saveFile.FileName))
                {
                    writer.Write(text.editString);
                }
                text.originalString = text.editString;
                return true;
            }
            else return false;
        }

        private bool Check()
        {
            bool check = false;
            text.editString = textBox.Text;
            if (text.originalString == text.editString)
            {
                check = true;
            }
            else
            {
                DialogResult dr = MessageBox.Show("Вы хотите сохранить изменения в файле?", "Блокнот", MessageBoxButtons.YesNoCancel);
                if (dr == DialogResult.Cancel)
                {
                    check = false;
                }

                if (dr == DialogResult.Yes)
                {
                    if (Save())
                    {
                        check = true;
                    }
                    else check = false;
                }
                if (dr == DialogResult.No)
                {
                    textBox.Clear();
                    text.Clear();
                    check = true;
                }
            }
            return check;
        }

        private void Form1_FormClosing(FormClosingEventArgs e)
        {
            if (MessageBox.Show("Закрыть?", "Message", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                e.Cancel = true;
            else
                e.Cancel = false;
        }

        private void правкаToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (textBox.Text != text.originalString)
            {
                CancelToolStripMenuItem.Enabled = true;
            }
            if (textBox.SelectedText != "")
            {
                CutToolStripMenuItem.Enabled = true;
                DeleteToolStripMenuItem.Enabled = true;
            }
        }
    }
}
